package cn.itcast.nems.product.scheduler;

import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.common.tenant.TenantInfo;
import cn.itcast.nems.common.tenant.TenantInfoHolder;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 刷新产品价格任务
 *
 * @author yong.li
 * create: 2023-08-28 18:30:14
 **/
@Component
public class FlushProductPriceJob {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final TuitionFeeService tuitionFeeService;

    public FlushProductPriceJob(TuitionFeeService tuitionFeeService) {
        this.tuitionFeeService = tuitionFeeService;
    }

    @XxlJob("flushProductPrice")
    public ReturnT<String> flushProductPrice(String param) {
        logger.warn("Flush product price job start with params {}...", param);
        boolean flushAll = false;
        int clazzId = 0;
        if (StringUtils.hasText(param)) {
            try {
                clazzId = Integer.parseInt(param.trim());
            } catch (Exception e) {
                flushAll = Boolean.parseBoolean(param.trim());
            }
        }
        try {
            AccountHolder.setSchedulerAccountInfo();
            TenantInfoHolder.setTenantInfo(new TenantInfo(TenantInfo.DEFAULT_TENANT_ID, TenantInfo.DEFAULT_SUBTENANT_ID));
            this.tuitionFeeService.flushProductPrice(flushAll, clazzId);
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        } finally {
            TenantInfoHolder.clear();
            AccountHolder.clear();
        }
        return new ReturnT<>("OK");
    }
}
